package com.tenbit.emp.svc;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tenbit.emp.dao.EmployeeEDAO;
import com.tenbit.emp.dao.EmployeeSkillEDAO;
import com.tenbit.emp.vo.EMP1010VO;

import emp.tenbit.emp.bean.EmpSkillBean;

@Component
public class EMP1010SVC {
	@Autowired
	private EmployeeEDAO edao;
	@Autowired
	private EmployeeSkillEDAO skillEDAO;
	private Logger log=Logger.getLogger(this.getClass());
	private String errMsg;
	
	public String getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public boolean doValidateGradDate(String sDate,String eDate){
		int sYear=Integer.parseInt(sDate.substring(0, 4));
		int sMonth=Integer.parseInt(sDate.substring(5, 7));
		int sDay=Integer.parseInt(sDate.substring(8));
		int eYear=Integer.parseInt(eDate.substring(0, 4));
		int eMonth=Integer.parseInt(eDate.substring(5, 7));
		int eDay=Integer.parseInt(eDate.substring(8));
		
		if(sYear > eYear){
			return false;
		}else if(sYear == eYear){
			if(sMonth > eMonth){
				return false;
			}else if(sMonth == eMonth){
				if(sDay > eDay){
					return false;
				}
			}
		}
		
		return true;
	}
	
	public boolean doValidateForm(EMP1010VO emp){
		boolean bName=emp.getName().length() != 0 ? true : false;
		boolean bPearsonId=emp.getPerson_id().length() !=0 ? true : false;
		boolean bDeptCode=emp.getDept_code().length() !=0 ? true : false;
		boolean bAcademicCode=emp.getAcademic_code() != null ? true : false;
		boolean bSkillCode=emp.getSkill_code() != null ? true : false;
		boolean bDate= !(emp.getGrad_sdate().equals("--") || emp.getGrad_edate().equals("--") );
		boolean bSalary=emp.getSalary() != null ? true : false;
		
		if( bName == false ){
			errMsg = "이름이 입력되지 않았습니다.";
			return false;
		}
		
		if( bPearsonId == false){
			errMsg = "주민번호를 다시 입력해주세요";
			return false;
		}
		
		if( bDeptCode == false){
			errMsg = "소속부서가 입력되지 않았습니다.";
			return false;
		}
		
		if( bAcademicCode == false){
			errMsg = "학력이 입력되지 않았습니다.";
			return false;
		} 
		
		if( bSkillCode == false){
			errMsg = "기술이 입력되지 않았습니다.";
			return false;
		}
		
		if( bDate == false){
			errMsg = "날짜를 입력해주세요";
			return false;
		}
		
		if( doValidateGradDate(emp.getGrad_sdate(), emp.getGrad_edate()) == false){
			errMsg = "시작날짜가 마지막날짜보다 큽니다.";
			return false;
		}
		
		if( bSalary == false){
			errMsg = "기준 급여가 입력되지 않았습니다.";
			return false;
		}
		
		return true;
	}
	public Map<String, Object> doMakeView(EMP1010VO emp){
		Map<String, Object> map=new HashMap<String, Object>();
		
		log.debug("doMakeView"+emp);
		map.put("name", emp.getName());
		
		if(emp.getPerson_id().equals("")){
			map.put("person_id1", "");
			map.put("person_id2", "");

		}else{
			map.put("person_id1", emp.getPerson_id().substring(0, 6));
			map.put("person_id2", emp.getPerson_id().substring(6));
			
		}
		
		map.put("dept_code", emp.getDept_code());
		map.put("academic_code",emp.getAcademic_code());
		map.put("sk",emp.getSkill_code());
		
		if(emp.getGrad_sdate().equals("--")){
			map.put("grad_stime_year", "");
			map.put("grad_stime_month","");
			map.put("grad_stime_day", "");

		}else{
			map.put("grad_stime_year", emp.getGrad_sdate().substring(0,4));
			map.put("grad_stime_month", emp.getGrad_sdate().substring(5,7));
			map.put("grad_stime_day", emp.getGrad_sdate().substring(8));
			
		}
		
		if(emp.getGrad_edate().equals("--")){
			map.put("grad_etime_year", "");
			map.put("grad_etime_month", "");
			map.put("grad_etime_day", "");

		}else{
			map.put("grad_etime_year", emp.getGrad_edate().substring(0,4));
			map.put("grad_etime_month", emp.getGrad_edate().substring(5,7));
			map.put("grad_etime_day", emp.getGrad_edate().substring(8));
			
		}
		map.put("salary", emp.getSalary());
		return map;
		
	}
	public boolean setEmployee(EMP1010VO emp){
		if(!doValidateForm(emp)){
			return false;
		}
		
		emp.setSex_code(doFindSexCode(emp));
		edao.updateEmploye(emp);
		skillEDAO.deleteEmpSkill(emp.getEmployee_id());
		
		for (int i = 0; i < emp.getSkill_code().size(); i++) {
			EmpSkillBean bean=new EmpSkillBean();
			
			bean.setEmployee_id(emp.getEmployee_id());
			bean.setSkill_code(emp.getSkill_code().get(i));
			
			skillEDAO.insertEmpSkill(bean);			
		}
		
		return true;
	}
	public void delEmployee(String emp_id){
		log.debug("========================> delEmployee "+emp_id);
		edao.deleteEmployee(emp_id);
		skillEDAO.deleteEmpSkill(emp_id);
	}
	public String doFindSexCode(EMP1010VO emp){
		String sex_code=null;
		String sex=emp.getPerson_id().substring(6,7);
		
		if(sex.equals("1")){
			sex_code="S1";
		}else{
			sex_code="S2";
		}
		return sex_code;
	}
	public boolean insEmployee(EMP1010VO emp){		
		if(!doValidateForm(emp)){
			return false;
		}
		
		emp.setSex_code(doFindSexCode(emp));
		emp.setEmployee_id(edao.selectEmployeeSeq());
		
		log.debug("insEmployee"+emp);
		edao.insertEmployee(emp);
		for (int i = 0; i < emp.getSkill_code().size(); i++) {
			EmpSkillBean bean=new EmpSkillBean();
			
			bean.setEmployee_id(emp.getEmployee_id());
			bean.setSkill_code(emp.getSkill_code().get(i));
			
			skillEDAO.insertEmpSkill(bean);			
		}
		
		return true;
	}
	
}
